<?php

namespace App\Http\Controllers;

use Illuminate\Support\Str;


class ApiToken extends Controller
{
    /**
     * 更新已经验证过的用户的 API 令牌。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    static function update(\App\User $user)
    {
        $token = Str::random(60);

        $user->forceFill([
            'api_token' => hash('sha256', $token),
        ])->save();

        return $token;//token的明文返回给前端
    }



    protected $oauth_site = 'http://blog.martin.net';

    /**
     * 通过账号密码获取token  密码授权
     */
    public function getToken()
    {
        $http = new \GuzzleHttp\Client;

        $response = $http->post($this->oauth_site.'/oauth/token', [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => 2,
                'client_secret' => 'n2wVVpi2oukFGRQBxdUeoX8gPi9BG9z1UcFVyRMy',
                'username' => 'wbz@qq.com',
                'password' => 'sd117532',
                'scope' => '',
            ],
        ]);

        return json_decode((string) $response->getBody(), true);
    }


}
